package com.jlastu.dlxy_androidapp.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.view.details.CommodityDetails;

import java.util.List;

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.ViewHolder> {

   private List<Goods> mList;

   private List<Photo> mList_P;


   public GoodsListAdapter(List<Goods> list , List<Photo> list_P){
      mList = list;
      mList_P = list_P;

   }

   @SuppressLint("NotifyDataSetChanged")
   public void updateData(List<Goods> list, List<Photo> list_P) {
      mList.clear();
      mList = list;
      mList_P=list_P;
      notifyDataSetChanged();
   }

   @NonNull
   @Override
   public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_item, null);
      ViewHolder viewHolder = new ViewHolder(view);
      return viewHolder;
   }


   @SuppressLint("DefaultLocale")
   public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
      Goods goods = mList.get(position);
      holder.textView1.setText(goods.getName());
      holder.textView2.setText(String.format("$ %.2f/%s", goods.getPrice(),goods.getUnit()));
      if ( position >= mList.size() || position >= mList_P.size()) {
         // 显示错误图标
          holder.photo.setImageResource(R.drawable.attention);
         return;
      }
      Photo photo = mList_P.get(position);
      holder.photo.setImageBitmap(photo.getBitmap());
   }

   @Override
   public int getItemCount() {
      return mList.size();
   }

   public class ViewHolder extends RecyclerView.ViewHolder {
      ImageView photo;
      TextView textView1;
      TextView textView2;

      public ViewHolder(@NonNull View itemView) {
         super(itemView);
         itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // 获取点击的列表项位置
               int position = getAdapterPosition();
               if (position != RecyclerView.NO_POSITION) {
                  // 处理点击事件，例如启动新的Activity
                  Goods goods = mList.get(position);
                  Intent intent = new Intent(itemView.getContext(), CommodityDetails.class);
                  intent.putExtra("goods_id", goods.getId());
                  itemView.getContext().startActivity(intent);

               }
            }
         });
         photo = itemView.findViewById(R.id.goods_photo);
         textView1 = itemView.findViewById(R.id.goods_name);
         textView2 = itemView.findViewById(R.id.goods_price);
      }
   }
}
